package cn.itcast.bos.service.impl.system;

import java.util.List;

import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.system.RoleRepository;
import cn.itcast.bos.dao.system.UserRepository;
import cn.itcast.bos.domain.system.Role;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private RoleRepository roleRepository;

	@Override
	public List<User> findByUser() {
		// TODO Auto-generated method stub
		return userRepository.findAll();
	}

	@Override
	public void saveUser(User user, String[] roleIds) {
		// *****************使用shiro默认强加密**********************************
		DefaultPasswordService passwordService = new DefaultPasswordService();
		String encryptPassword = passwordService.encryptPassword(user.getPassword());
		user.setPassword(encryptPassword);
		// *****************使用shiro默认强加密********************************** //
		// TODO Auto-generated method stub
		// 保存用户
		userRepository.save(user);
		// 判断角色id
		if (roleIds != null) {
			// 循环遍历获取角色id
			for (String roleId : roleIds) {
				Role role = roleRepository.findOne(Integer.parseInt(roleId));
				// 关联角色
				user.getRoles().add(role);
				// 等快照更新
			}
		}
	}

	@Override
	public void updateUserStatusByActivetimeForLock() {
		// TODO Auto-generated method stub
		userRepository.updateUserStatusByActivetimeForLock(3);
	}

}
